package controllers.mail.models;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;

import controllers.Log;

import person.Employee;
import person.Person;

import budget.Request;


public class CModels {
	public static String getRegisterMailModel(Person person,String username,String password){
		String loaded = loadModel(REGISTRATION_MAIL_MODEL);
		loaded=loaded.replaceAll("@PERSON_NAME", person.getName());
		loaded=loaded.replaceAll("@PERSON_SURNAME", person.getSurname());
		loaded=loaded.replaceAll("@USERNAME", username);
		loaded=loaded.replaceAll("@PASSWORD", password);
		loaded=loaded.replaceAll("@CATERINA_STYLE", getCaterinaStyle());
		return loaded;
	}
	public static String getRequestAcceptedModel(Employee employee,Request req){
		String loaded = loadModel(ACCEPTED_REQUEST_MAIL_MODEL);
		loaded=loaded.replaceAll("@PERSON_NAME", req.getCustomer().getName());
		loaded=loaded.replaceAll("@PERSON_SURNAME", req.getCustomer().getSurname());
		loaded=loaded.replaceAll("@RAPR_NAME", employee.getName());
		loaded=loaded.replaceAll("@RAPR_SURNAME", employee.getSurname());
		loaded=loaded.replaceAll("@RAPR_MAIL", employee.getEmail());
		loaded=loaded.replaceAll("@EVENT_NAME", req.getEvent().getEventName());
		loaded=loaded.replaceAll("@EVENT_TYPE", req.getEvent().getEventType().name());
		loaded=loaded.replaceAll("@EVENT_PREPARATION", req.getEvent().getPreparationType().name());
		loaded=loaded.replaceAll("@EVENT_SERVICE", req.getEvent().getServiceType().name());
		loaded=loaded.replaceAll("@EVENT_START", getFormattedDate(req.getEvent().getBeginDate()));
		loaded=loaded.replaceAll("@EVENT_END", getFormattedDate(req.getEvent().getEndDate()));
		loaded=loaded.replaceAll("@EVENT_REQ_DATE", getFormattedDate(req.getRequestDate()));
		loaded=loaded.replaceAll("@EVENT_LOCATION", req.getEvent().getPlace());
		loaded=loaded.replaceAll("@EVENT_GUESTS", Integer.toString(req.getEvent().getGuestsNumber()));
		loaded=loaded.replaceAll("@EVENT_DETAILS", req.getMemo());
		return loaded;
	}
	private static String getCaterinaStyle(){
		String style="<style type=\"text/css\">";
		style+=loadModel(CATERINA_STYLE);
		style+="</style>";
		return style;
	}
	public static String getFormattedDate(Calendar cal){
		if (cal!=null){
		StringBuilder sb=new StringBuilder();
		sb.append(cal.get(Calendar.DATE)+" ");
		sb.append(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ITALY)+" ");
		sb.append(cal.get(Calendar.YEAR));
		return sb.toString();
		}
		return null;
	}
	private static String loadModel(String modelPath){
		try {
		Scanner sc = new Scanner(new File(modelPath));
		StringBuilder sb=new StringBuilder();
		while(sc.hasNextLine())sb.append(sc.nextLine());
		sc.close();
		return sb.toString();
		} catch (FileNotFoundException e) {Log.print(e);return "Errore durante il caricamento del modello";}
	}
	private static final String REGISTRATION_MAIL_MODEL="res/models/registerMail.html";
	private static final String ACCEPTED_REQUEST_MAIL_MODEL="res/models/reqMail.html";
	private static final String CATERINA_STYLE="res/models/caterinaStyle.css";
}
